package until

import (
	"until/yml"
	"github.com/kataras/iris"
	"until/db"
	"os"
	"path/filepath"
	"fmt"
	"tools/pid"
	"until/redgo"
	"until/logger"
	"until/validate"
)

func Untilinit(dir string,app *iris.Application) {
	yml.GetConf(dir+"/gocronn.yml")
	yml.Dir=dir
	logger.Init(app.Logger())
	validate.InitVal()
	db.SetOrm()
	//db.SetSqlx()
	redgo.StartRp()
	pid(yml.Config.Pid)
}
//pid文件书写
func pid(pid string)  {
	tmp:=os.TempDir()
	_, err := os.Open(tmp)
	if err != nil {
		if os.IsNotExist(err){
			err := os.Mkdir(tmp, os.ModePerm)
			if err != nil {
				panic(err)
			}
		}
	}
	mypidfile:=tmp+string(filepath.Separator)+pid
	fmt.Println("pid:",mypidfile)
	err=pidfile.Write(mypidfile)
	if err!=nil {
		panic(err)
	}
}
